import feedparser
import md5
import os.path 
import re
import sys
from urllib import urlretrieve

verbose = 1

if len(sys.argv) < 3:
    print "%s <rss_url> <directory_to_mirror>"%sys.argv[0]
    sys.exit(-1)

feed=sys.argv[1]
directory=sys.argv[2]

if verbose:
    print "Got feed url of %s"%feed
    print "Got mirror directory of %s"%directory

# figure out the feed directory name
digest = md5.new()
digest.update(feed);
feed_dir = directory + os.sep + digest.hexdigest()

if verbose:
    print "Feed directory of %s"%feed_dir
    
# ensure directory exists
if not os.path.exists(directory):
    os.mkdir(directory)
if not os.path.exists(feed_dir):
    os.mkdir(feed_dir)
    

fileext_re = re.compile("^.*\.([^.]+)$")

feed_files = []

feed = feedparser.parse(feed)
for entry in feed['entries']:
    if 'enclosures' in entry:
        for enclosure in entry.enclosures:
            url = enclosure.href
            match = fileext_re.match(url)
            if match:
                ext = match.group(1)    
                digest = md5.new()
                digest.update(url)
                filename = feed_dir +  os.sep + digest.hexdigest() + "." + ext
                feed_files.append((url,filename))

if verbose:
    print "Feed entries and filenames:"
    for tp in feed_files:
        print "\t%s\t%s"%tp

existing_files = {}
files_to_download = {}
for path in os.listdir(feed_dir):
    existing_files[feed_dir + os.sep + path] = 1

for tp in feed_files:
    url,filepath = tp
    # no need to delete existing files
    if existing_files.__contains__(filepath):
        if verbose:
            print "Retaining %s, already downloaded"%filepath
        del existing_files[filepath]
    else:
        files_to_download[filepath] = url
        
# delete the guys to delete
for filepath in existing_files.keys():
    if verbose:
        print "Deleting %s, no longer in the feed"%filepath
    os.unlink(filepath)
    
for filepath in files_to_download.keys():
    url = files_to_download[filepath]
    if verbose:
        print "Downloading %s to %s"%(url,filepath)
    urlretrieve(url, filepath)
        
